<?php
namespace app\model;

use think\model\relation\BelongsTo;

/*
 *  点赞模型
 */
class UserUpvote extends \think\Model
{
    protected $table = 'user_upvote';      // 点赞
    /**
     * 自动时间戳数据类型
     *
     * @var string
     */
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'create_at';
    protected $updateTime = false;

    protected $field = ['user_id', 'dynamic_id']; 
    // 定义全局的查询范围
    // protected $globalScope = ['user'];
    // 查询范围
    public function scopeUser($query)
    {
        $query->where('user_id', request()->uid);
    }
    /**
     * 用户
     *
     * @return void
     */
    public function user() : BelongsTo
    {
        return $this->belongsTo('User');
    }
    /**
     * 点赞的动态
     *
     * @return void
     */
    public function dynamic() : BelongsTo
    {
        return $this->belongsTo('Dynamic', 'dynamic_id');
    }
    public function searchUserIdAttr($query, $value, $data)
    {
        $value > 0 && $query->where('user_id', $value);
    }
    public function searchDynamicIdAttr($query, $value, $data)
    {
        $value > 0 && $query->where('dynamic_id', $value);
    }
    /*
     * 新增前
     */
    public static function onBeforeInsert($model)
    {
        $model->user_id = request()->uid;
    }
}
